import { Context } from 'egg';

export default () => {
    return async (ctx: Context, next: () => Promise<any>) => {
        try {
            await next();
        } catch (err) {
            const status = err.status;
            const msg = status === 500 && ctx.app.config.env === 'prod'
                ? '服务器内部错误'
                : err.errorMsg ? err.errorMsg : err.message;
            const resCode = err.errorCode || -999;
            ctx.body = {
                resCode,
                resMsg: msg,
                data: {},
            };
            ctx.status = 200;
        }
    };
};
